﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.IO;


namespace backoffice
{
    public partial class backoffice : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
          //  _Init();
            GenerateScript();

        }
        public void _Init()
        { 
            //Test
            backofficereport[] row = backofficereport.InitArray(5);
            row[0].CustomersTotal = 20;
            row[0].CustumersForEachCashBox = 200;
            row[0].TimeUnit = TimeSpan.Parse("07:00");

            
            row[1].CustomersTotal = 200;
            row[1].CustumersForEachCashBox = 200;
            row[1].TimeUnit = TimeSpan.Parse("08:00");


            row[2].CustomersTotal = 200;
            row[2].CustumersForEachCashBox = 200;
            row[2].TimeUnit = TimeSpan.Parse("09:00");

            row[3].CustomersTotal = 300;
            row[3].CustumersForEachCashBox = 300;
            row[3].TimeUnit = TimeSpan.Parse("10:00");

            row[4].CustomersTotal = 330;
            row[4].CustumersForEachCashBox = 330;
            row[4].TimeUnit = TimeSpan.Parse("11:00");

           

            backofficecomponent testunit = new backofficecomponent();
            
            testunit.SetInterval(row[0], row[1]);
            testunit.MinKassa = 1;
            testunit.CustomerPerCashBox = 200;

            //javascript output
            StringBuilder js = backofficecomponent.JSinit();

            js = backofficecomponent.JSNumberOfRows(5, js);
            int counter = 0;
           
         
            foreach (backofficereport i in row)
            {
                backofficecomponent.JSAddStringRow(counter,0, i.TimeUnit.Hours.ToString(), js);
                backofficecomponent.JSAddIntRow(counter,1,testunit.GetCashBoxPerInterval(i).ToString(),js);
                counter++;
            }
            js = backofficecomponent.JSFinalize(js);

            ClientScript.RegisterClientScriptBlock(this.GetType(), "Script", js.ToString());
            
        }
        private void GenerateScript()
        { 
                
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            HtmlGenericControl h1 = new HtmlGenericControl("div");
            h1.ID = "chart_div2";
            PlaceHolder1.Controls.Add(h1);

            backofficecomponent Reporter= new backofficecomponent();
            int NumberOfRows = -1;
            if (FileUpload1.HasFile)
                try
                {
                    
                   
                    string path = Server.MapPath("backoffceupload") + @"\" + FileUpload1.PostedFile.FileName;

                    //check file extention 
                    //add error handler
                    if (System.IO.Path.GetExtension(this.FileUpload1.PostedFile.FileName).ToLower() == ".xls" ||
                        System.IO.Path.GetExtension(this.FileUpload1.PostedFile.FileName).ToLower() == ".xlsx")
                    {
                            FileUpload1.PostedFile.SaveAs(path);
                    }
                    else { 
                    
                            //thorw a file type error exception. 
                    }
                    Label2.Text = File.Exists(path) ? "File exists at : " + path : "File does not exist. at " + path;
                    if (File.Exists(path))
                    {


                        DataTable table = new DataTable();
                        table = Reporter.ExcelReader(path);
                        GridView1.DataSource = table;
                        GridView1.DataBind();
                        //int t = Reporter.ExcelFileRowCount;
                        //Response.Write(t.ToString());
                        NumberOfRows = Reporter.ExcelFileRowCount;
                        backofficereport[] row = backofficereport.InitArray(Reporter.ExcelFileRowCount);
                        try
                        {
                           
                            for (int i = 1; i <Reporter.ExcelFileRowCount; i++)
                            {
                                row[i].CustomerId = 3; //get this from session or wherever it is stored
                                row[i].Date =DateTime.Parse( table.Rows[i]["F1"].ToString());
                                row[i].TimeUnit = TimeSpan.Parse(table.Rows[i]["F2"].ToString());
                                row[i].CustumersForEachCashBox =Convert.ToInt32( table.Rows[i]["F3"].ToString());
                                row[i].CustomersTotal=Convert.ToInt32(table.Rows[i]["F4"].ToString());
                            }
                        }
                        catch (Exception ex)
                        {
                            //add error handler, occurred if excel file field names dosn't match with the given data
                            Label1.Text = "1 : "+ex.Message.ToString();
                        }
                        try
                        {
                            backofficecomponent DataProvider = new backofficecomponent();

                            DataProvider.SetInterval(row[1], row[2]);
                            DataProvider.MinKassa = 1;  // get this from user input
                            DataProvider.CustomerPerCashBox = 200;  // get this from user input

                            //javascript output
                            StringBuilder js = backofficecomponent.JSinit();
                            if(NumberOfRows != -1)
                            { 
                                js = backofficecomponent.JSNumberOfRows(NumberOfRows, js);
                                int counter = 0;
                                //foreach (backofficereport i in row)
                                //{
                                //    backofficecomponent.JSAddStringRow(counter, 0, i.TimeUnit.Hours.ToString(), js);
                                //    backofficecomponent.JSAddIntRow(counter, 1, DataProvider.GetCashBoxPerInterval(i).ToString(), js);
                                //    counter++;
                                //    i.InsertDb();
                                //}
                                for (int i = 1; i < row.Length; i++)
                                {
                                    backofficecomponent.JSAddStringRow(counter, 0, row[i].TimeUnit.Hours.ToString(), js);
                                    backofficecomponent.JSAddIntRow(counter, 1, DataProvider.GetCashBoxPerInterval(row[i]).ToString(), js);
                                    counter++;
                                   // row[i].InsertDb();  // adding to db
                                }
                                js = backofficecomponent.JSFinalize(js);
                                ClientScript.RegisterClientScriptBlock(this.GetType(), "Script", js.ToString());
                            }
                        }
                        catch (Exception ex)// java script builder
                        {
                            Label1.Text = "2 : " + ex.Message.ToString();
                        }
                        
                    }
                  
                }
                catch (Exception ex)
                {
                    Label1.Text = "3 : " + ex.Message.ToString();
                }
               
        }
    }
}
